package com.example.demo.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
//用于处理xls文件
public class ImportQuestion {
    List<String[]>questionList = new ArrayList<>();

    public List<String[]> getQuestionList() {
        return questionList;
    }
    public int getSize(){
        return questionList.size();
    }
    public ImportQuestion(MultipartFile file){
//        InputStream in = new FileInputStream(fileName);
//        InputStream inputStream = new FileInputStream(fileName)
        String fileName = file.getOriginalFilename();
        System.out.println("进入到文件处理"+fileName);
        try (InputStream inputStream = file.getInputStream()) {
            Workbook workbook = null;
            System.out.println("6666");
            int i=fileName.lastIndexOf('.');
            String category=fileName.substring(i+1);
            // 根据文件扩展名或内容类型选择合适的Workbook工厂方法0-
            if (category.equals("xls")) {
                System.out.println("进入到了");
                workbook = new HSSFWorkbook(inputStream); // 用于旧版Excel文件 (.xls)
            } else if (category.equals("xlsx")) {
                workbook = new XSSFWorkbook(inputStream); // 用于新版Excel文件 (.xlsx)
            }
            //获取第一个文件
            Sheet sheet = workbook.getSheetAt(0);
            for(Row row :sheet){
                if(!isRowEmpty(row)){
                    String[] rowValues = getRowValues(row);
                    this.questionList.add(rowValues);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //检查行是否为空
    private boolean isRowEmpty(Row row) {
        for (Cell cell : row) {
            if (cell.getCellType() != CellType.BLANK) {
                return false;
            }
        }
        return true;
    }
    //获取行值
    public String[] getRowValues(Row row) {
        String[] values = new String[row.getLastCellNum()]; // 创建一个足够大的数组
        for (int i = 0; i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            // 确保跳过空单元格或错误类型的单元格
            if (cell != null) {
                values[i] = getCellValue(cell);
            }
        }
        return values;
    }
    //获取单元格值
    public String getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
            default:
                return "";
        }
    }

    public static String formatJavaClass(String str) {
        if(str==null){
            return null;
        }
        boolean isFirstClass = true; // 标志变量，用于跟踪是否是第一个 class
        StringBuilder formattedStr = new StringBuilder();
        // 替换第一个 class 关键字
        if(str.contains("class")){
            int classIndex = str.indexOf("class");
            if (classIndex != -1 ) {
                formattedStr.append("\n");
                formattedStr.append(str.substring(0, classIndex + 5)); // "class".length() 是 5
                str = str.substring(classIndex + 5);
            }
        }
        formattedStr.append(str);
        // 替换连续的五个或更多空格
        formattedStr = new StringBuilder(formattedStr.toString().replace("     ", "\n"));

        // 替换右大括号
        formattedStr = new StringBuilder(formattedStr.toString().replaceAll("}", "}\n"));

        // 替换换行符
        String result = formattedStr.toString().replaceAll("\n", "</p><p>");

        return "<p>" + result + "</p>";
    }
}